package com.yoga.manager.admin.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.TeacherBO;
import com.yoga.common.page.PageBO;
import com.yoga.common.qto.TeacherQTO;
import com.yoga.dao.dao.TeacherDao;
import com.yoga.dao.domain.TeacherDO;
import com.yoga.manager.admin.AdminTeacherManager;
import com.yoga.manager.beanUtil.TeacherBeanUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hdx on 2020/10/19.
 */
@Component
public class AdminTeacherManagerImpl implements AdminTeacherManager {
    Logger logger = LoggerFactory.getLogger(AdminTeacherManagerImpl.class);

    @Resource
    private TeacherDao teacherDao;

    @Override
    public TeacherBO addData(TeacherBO teacherBO) throws AppException {
        if (teacherBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(teacherBO.getTeacherName())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(teacherBO.getPhone())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(teacherBO.getRealName())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(teacherBO.getTeacherImg())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        TeacherDO teacherDO = TeacherBeanUtil.teacherBOToDO(teacherBO);
        int count = teacherDao.insert(teacherDO);
        if (count <= 0) {
            throw new AppException(AppErrorCodeEnum.INSERT_DATA_NULL);
        }

        return TeacherBeanUtil.teacherDOToBO(teacherDao.findById(teacherDO.getId()));
    }

    @Override
    public Boolean updateData(TeacherBO teacherBO) throws AppException {
        if (teacherBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (teacherBO.getTeacherId() == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        int num = teacherDao.updateById(TeacherBeanUtil.teacherBOToDO(teacherBO));
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return true;
    }

    @Override
    public Boolean deleteData(Long teacherId) throws AppException {
        if (teacherId == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        int num = teacherDao.deleteById(teacherId);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return true;
    }

    @Override
    public PageBO<TeacherBO> findForPage(TeacherQTO teacherQTO) throws AppException {
        if (teacherQTO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        PageBO<TeacherBO> pageBO = new PageBO<>();
        pageBO.setQuery(teacherQTO);
        Integer count = teacherDao.findForPageCount(teacherQTO);
        if (count == null || count <= 0) {
            return pageBO;
        }
        List<TeacherBO> boList = TeacherBeanUtil.doList2BOList(teacherDao.findForPage(teacherQTO));
        pageBO.setTotalCount(count);
        pageBO.setData(boList);

        return pageBO;
    }
}
